﻿/*===================================================
* CLR版本  : 4.0.30319.42000 
* 类名称   : Entity
* 文 件 名 : Entity 
* 类描述   :
* 创建人   : oream
* 创建时间 : 2018/3/7 21:49:48
* 修改人   : 
* 修改时间 :
* 版本： @version 1.0
=====================================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using UnityEngine;

namespace Hotfix
{
  public class Entity : Component
  {
    private Dictionary<Type, Component> dicComponentDir = new Dictionary<Type, Component>();


    public Entity()
    {
      this.Id = IDGenerater.GenerateId();
    }

    public Entity(long id)
    {
      this.Id = id;
    }

    public override void Dispose()
    {
      if (this.Id == 0)
      {
        return;
      }
      base.Dispose();
      foreach (var item in dicComponentDir.Values)
      {
        try
        {
          item.Dispose();
        }
        catch (Exception e)
        {
          Debug.LogError(e.ToString());
        }
      }

      this.ClearAllComponent();
    }


    #region AddComponenct
    public T AddComponent<T>() where T : Component
    {
      T tmp = ComponentFactory.Create<T>(this);
      if (!dicComponentDir.ContainsKey(tmp.GetType()))
      {
        throw new Exception($"AddComponent Error, component existing ! id :{tmp.Id} ,component : {tmp.GetType().Name}");
      }
      this.dicComponentDir.Add(tmp.GetType(), tmp);
      return tmp;
    }
    public T1 AddComponent<T1, T2>(T2 t2) where T1 : Component
    {
      T1 tmp = ComponentFactory.Create<T1, T2>(this, t2);
      if (!dicComponentDir.ContainsKey(tmp.GetType()))
      {
        throw new Exception($"AddComponent Error, component existing ! id :{tmp.Id} ,component : {tmp.GetType().Name}");
      }
      this.dicComponentDir.Add(tmp.GetType(), tmp);
      return tmp;
    }
    public T1 AddComponent<T1, T2, T3>(T2 t2, T3 t3) where T1 : Component
    {
      T1 tmp = ComponentFactory.Create<T1, T2, T3>(this, t2, t3);
      if (!dicComponentDir.ContainsKey(tmp.GetType()))
      {
        throw new Exception($"AddComponent Error, component existing ! id :{tmp.Id} ,component : {tmp.GetType().Name}");
      }
      this.dicComponentDir.Add(tmp.GetType(), tmp);
      return tmp;
    }
    public T1 AddComponent<T1, T2, T3, T4>(T2 t2, T3 t3, T4 t4) where T1 : Component
    {
      T1 tmp = ComponentFactory.Create<T1, T2, T3, T4>(this, t2, t3, t4);
      if (!dicComponentDir.ContainsKey(tmp.GetType()))
      {
        throw new Exception($"AddComponent Error, component existing ! id :{tmp.Id} ,component : {tmp.GetType().Name}");
      }
      this.dicComponentDir.Add(tmp.GetType(), tmp);
      return tmp;
    }
    #endregion


    public T GetComponent<T>() where T : Component
    {
      Component component;
      if (!this.dicComponentDir.TryGetValue(typeof(T), out component))
      {
        return null;
      }
      return (T)component;
    }

    public Component[] GetAllComponent()
    {
      return this.dicComponentDir.Values.ToArray();
    }

    public void ClearAllComponent()
    {
      this.dicComponentDir.Clear();
    }

    public void RemoveComponent<T>()
    {
      this.RemoveComponent(typeof(T));
    }

    public void RemoveComponent(Type type)
    {
      if (this.dicComponentDir.ContainsKey(type))
      {
        this.dicComponentDir.Remove(type);
      }
    }
  }
}
